package com.hardydou.framework.filestore;

import cn.hutool.core.util.StrUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Struct;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;

/**
 * @author ：hardy
 * @description：文件存储
 * @date ：Created in 2020/6/15
 * @Copyright ：Hardydou.com
 */
public abstract class AbstractFileStore {
    protected LocalDate date = LocalDate.now();
    protected LocalTime time = LocalTime.now();
    protected FileStoreConfig config;
    protected BusType busType;
    protected FileType fileType;
    protected String extName;

    private final static Map<FileType, List<String>> SAFE_TYPE = new HashMap<>();

    static {
        SAFE_TYPE.put(FileType.IMAGE, Arrays.asList("jpg,gif,png,bmp,png".split(",")));
    }

    public AbstractFileStore(FileStoreConfig config, FileType fileType, BusType busType, String extName) {
        this.config = config;
        this.fileType = fileType;
        this.busType = busType;
        this.extName = extName;
    }

    /**
     * @author ：hardy
     * @description：计算相对路径
     * @date ：Created in 2020/6/15
     * @Copyright ：Hardydou.com
     */
    public String calRelativePath() {
        return new StringBuilder("")
                .append(File.separator).append(fileType)
                .append(File.separator).append(busType)
                .append(File.separator)
                .append(date.getYear())
                .append(File.separator)
                .append(date.getDayOfYear())
                .append(File.separator)
                .append(time.toNanoOfDay())
                .append(".").append(extName).toString();
    }

    public boolean isSafeExt() {
        List<String> tmp = SAFE_TYPE.get(fileType);
        if (Objects.isNull(tmp)) {
            return true;
        }
        return tmp.contains(extName.toLowerCase());
    }

    public String getPathPreFix() {
        return StrUtil.subBefore(config.getPathPattern(), "/", true);
    }

    public abstract String getReqPath();

    public abstract OutputStream createFile() throws IOException;

    public abstract void write(InputStream in) throws IOException;

    public abstract OutputStream read(String relativePath) throws IOException;


    public static enum FileType {
        NONE,
        IMAGE,
        EXCEL;

        public static FileType value(String val) {
            FileType[] vals = values();
            for (int i = 0; i < vals.length; i++) {
                if (StrUtil.equals(val, vals[i].name(), true)) {
                    return vals[i];
                }
            }
            return NONE;
        }

        @Override
        public String toString() {
            return name().toLowerCase();
        }
    }

    public static enum BusType {
        NONE,
        AVATAR,
        QRCODE;

        public static BusType value(String val) {
            BusType[] vals = values();
            for (int i = 0; i < vals.length; i++) {
                if (StrUtil.equals(val, vals[i].name(), true)) {
                    return vals[i];
                }
            }
            return NONE;
        }

        @Override
        public String toString() {
            return name().toLowerCase();
        }
    }

}
